#include <allegro.h>
#include <stdio.h>
#include <string.h>
#include"fonctions.h"
 
int main() 
{

	BITMAP * Buffer=NULL;
        
        char chemin_background[]="./Images/page1.bmp";
        char chemin_background2[]="./Images/page1_collision.bmp";
	BITMAP *Background=NULL;
	BITMAP *BackgroundCol=NULL;
        char chemin_background_p2[]="./Images/page2.bmp";
        char chemin_background2_p2[]="./Images/page2_collision.bmp";
	BITMAP *Background_p2=NULL;
	BITMAP *BackgroundCol_p2=NULL;
        char chemin_background_p3[]="./Images/menu.bmp";
        char chemin_background2_p3[]="./Images/menu_collision.bmp";
	BITMAP *Background_p3=NULL;
	BITMAP *BackgroundCol_p3=NULL;
	int score=0;
	epine epine,epine2,epine3,epine5; 

	epine.x=1050;
	epine.y=230;

	epine2.x=3250;
	epine2.y=210;

	epine3.x=2550;
	epine3.y=210;

	epine5.x=2550;
	epine5.y=490;
	
	epine3.affiche=0;
	epine5.affiche=0;

        variable v;
	int dead=0;
	int move=0;

	int vie=3;
	int test=1;

	int en=200;
	int i=0;
        int j=0;
        int compteur;
        Personnage p;
compteur = 0;
init_grace(&p);
	/* General initialization function */
	allegro_init();
	if (install_keyboard() == -1) {
		allegro_message("Error! %s", allegro_error);
		return (-1);
	}
	set_color_depth(24);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_W, GFX_H, 0, 0) != 0) {
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* heres the command for text mode */
		allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
		return (1);
	}

	
extern volatile int mouse_x,mouse_y,mouse_b;

install_mouse(); //fonction pour disposer de la souris
 enable_hardware_cursor();
 select_mouse_cursor(MOUSE_CURSOR_BUSY);

show_mouse(screen); // pour voir la souris a l'ecran

       
        Buffer = create_bitmap(SCREEN_W, SCREEN_H);
	//loading background + background collision
	Background=load_background_bmp(chemin_background);
	//loading background collision Map
        BackgroundCol=load_background_bmp(chemin_background2);

	//loading background + background collision
	Background_p2=load_background_bmp(chemin_background_p2);
	//loading background collision Map
        BackgroundCol_p2=load_background_bmp(chemin_background2_p2);
	//loading background + background collision
	Background_p3=load_background_bmp(chemin_background_p3);
	//loading background collision Map
        BackgroundCol_p3=load_background_bmp(chemin_background2_p3);
        //loading Grace
	load_grace(&p);
        
   while (!key[KEY_ESC]) 
	{
		//vider le buffer
		clear_bitmap(Buffer);
		//drawing background
		draw_background(Background,Buffer,j);
                

   v.x=mouse_x;
   v.y=mouse_y;
   v.w=5;
   v.h=5;

   if ((collision_background_left(v,BackgroundCol,255))||(collision_background_right(v,BackgroundCol,255))||(collision_background_up(v,BackgroundCol,255))||(collision_background_down(v,BackgroundCol,255)))
{
if((mouse_b & 1)&&(mouse_x>300)&&(mouse_x<600)&&(mouse_y<350)&&(mouse_y>250))
{
Background = load_bitmap("./Images/page2.bmp", NULL);
}
}

   if ((collision_background_left(v,BackgroundCol_p2,255))||(collision_background_right(v,BackgroundCol_p2,255))||(collision_background_up(v,BackgroundCol_p2,255))||(collision_background_down(v,BackgroundCol_p2,255)))
{
if((mouse_b & 1)&&(mouse_x>310)&&(mouse_x<530)&&(mouse_y<800)&&(mouse_y>520))
{
Background = load_bitmap("./Images/menu.bmp", NULL);
}
 }

//bouton play

   if ((collision_background_left(v,BackgroundCol_p3,255))||(collision_background_right(v,BackgroundCol_p3,255))||(collision_background_up(v,BackgroundCol_p3,255))||(collision_background_down(v,BackgroundCol_p3,255)))
{
if((mouse_b & 1)&&(mouse_x>450)&&(mouse_x<750)&&(mouse_y<250)&&(mouse_y>150))
{
Background = load_bitmap("./Images/level_desert.bmp", NULL);
}
}

if((get_pixel(Background, v.x,v.y)==255)&&(mouse_b & 1)&&(mouse_x>10)&&(mouse_x<300)&&(mouse_y<400)&&(mouse_y>100))
{
Background = load_bitmap("./Images/city.bmp", NULL);
}

if((get_pixel(Background, v.x,v.y)==255)&&(mouse_b & 1)&&(mouse_x>600)&&(mouse_x<800)&&(mouse_y<400)&&(mouse_y>50))
{


     char chemin_background[]="./Pictures/foret.bmp";
     char chemin_background2[]="./Pictures/foretblanc.bmp";
      j=0;   
      epine.image=load_bitmap("./Pictures/epine.bmp", NULL);	
      epine.image1=load_bitmap("./Pictures/epine5.bmp", NULL);
      
      clear_bitmap(Buffer);
      textout_centre_ex(Buffer, font, "Partie 2 : foret cliquer sur S pour demarrer le jeu !", SCREEN_W/2,150, makecol(255, 0, 100), -1);
           
     

  if(key[KEY_S])
{   
Background=load_background_bmp(chemin_background);
BackgroundCol=load_background_bmp(chemin_background2);
init_grace(&p);

while(p.x<SCREEN_W - p.img_right[0]->w)
   {  
      clear_bitmap(Buffer);
      move_grace(&p,&compteur,BackgroundCol,&j,&move,&test);
      draw_background(Background,Buffer,j);

      		draw_grace(p,Buffer,compteur);
		textprintf_ex(Buffer, font, 300, 30, makecol(0,0,0),-1, " TEMPS: %f s", (float)clock()/CLOCKS_PER_SEC);
		textprintf_ex(Buffer,font,10,45,makecol(0,0,0),-1,"VIE: %d ",vie);
		textprintf_ex(Buffer,font,10,30,makecol(236,15,75),-1,"ENERGIE: %d ",en);

		textprintf_ex(Buffer,font,150,30,makecol(0,0,255),-1,"SCORE: %d ",score);

  		draw_sprite(Buffer, epine.image, epine.x,epine.y);
		draw_sprite(Buffer, epine.image, epine2.x,epine2.y);
		

if(epine3.affiche==1)
{draw_sprite(Buffer, epine.image, epine3.x,epine3.y);
 
} 

if(epine5.affiche==1)
{draw_sprite(Buffer, epine.image1, epine5.x,epine5.y);
 
}

if( (j<Background->w - SCREEN_W && j>=1000) && move!= 1)
{
  epine3.affiche=1;
}

if( (j<Background->w - SCREEN_W && j>=1000) && move== 1)
{
  epine3.affiche=1;
  epine3.x=epine3.x-3;
}


if( (j<Background->w - SCREEN_W && j>=1000) && move!= 1)
{
  epine5.affiche=1;
}

if( (j<Background->w - SCREEN_W && j>=1000) && move== 1)
{
  epine5.affiche=1;
  epine5.x=epine5.x-3;
}
if( move== 1)
{
	epine.x=epine.x-3;
	epine2.x=epine2.x-3;

}


if(p.y>=SCREEN_H)
{

 dead=1;
}

if( collision(p.x,p.y,p.img_right[0]->w,p.img_right[0]->h, epine.x,epine.y,epine.image->w, epine.image->h)==1)
{ dead=1;}
if( collision(p.x,p.y,p.img_right[0]->w,p.img_right[0]->h, epine2.x,epine2.y,epine.image->w, epine.image->h)==1)
{ dead=1;}

if( collision(p.x,p.y,p.img_right[0]->w,p.img_right[0]->h, epine3.x,epine3.y,epine.image->w, epine.image->h)==1)
{ dead=1;}

if( collision(p.x,p.y,p.img_right[0]->w,p.img_right[0]->h, epine5.x,epine5.y,epine.image1->w, epine.image1->h)==1)
{ dead=1;}


if (dead==1)
{
clear_bitmap(Buffer);
textout_centre_ex(Buffer, font, "You Loose a Life press r to Replay the level  !", SCREEN_W/2,SCREEN_H/2, makecol(255, 0, 100), -1);

}
 
if(dead==1)
{

en=en-1;

}
if (vie>0){

if(en<=0)
{
vie--;
en=200;}

}else{
	clear_bitmap(Buffer);
	textout_centre_ex(Buffer, font, "GAME OVER!", SCREEN_W/2,SCREEN_H/2, makecol(236,15,75), -1);
 }
     i=0;
     if(key[KEY_R])
       { //vie++; // 10 et 13 . 18 et 19 . 28 et 29
  	 j=0;
	 init_grace(&p);
         dead=0;
	
	epine3.affiche=0;
	epine.x=1050;
	epine.y=230;
	
	epine2.x=3250;
	epine2.y=210;

	epine3.x=2550;
	epine3.y=210;


	epine5.x=2550;
	epine5.y=490;
}
    
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
   }
}
}

if(p.x>820)
{
clear_bitmap(Buffer);
textout_centre_ex(Buffer, font, "Partie terminée! Press C to continue", SCREEN_W/2,150, makecol(255, 0, 100), -1);

//textprintf_ex(Buffer, font, 270, 200, makecol(0,0,255),-1, "vous avez mis %f secondes! ", temps);


if ((float)clock()/CLOCKS_PER_SEC<=10)
{score=100;
textprintf_ex(Buffer,font,270,240,makecol(0,0,255),-1,"SCORE: %d",score); // afficher texte a lecran
}
else if ((float)clock()/CLOCKS_PER_SEC<=20)
{score=80;
textprintf_ex(Buffer,font,270,240,makecol(0,0,255),-1,"SCORE: %d",score);
}
else if ((float)clock()/CLOCKS_PER_SEC<=40) {
score=60;
textprintf_ex(Buffer,font,270,240,makecol(0,0,255),-1,"SCORE: %d",score); }
else if ((float)clock()/CLOCKS_PER_SEC<=60) {
score=40;
textprintf_ex(Buffer,font,270,240,makecol(0,0,255),-1,"SCORE: %d",score); }
else if ((float)clock()/CLOCKS_PER_SEC<=80) {
score=20;
textprintf_ex(Buffer,font,270,240,makecol(0,0,255),-1,"SCORE: %d",score); }
else {
score=0;
textprintf_ex(Buffer,font,270,240,makecol(0,0,255),-1,"SCORE: %d",score); }

textprintf_ex(Buffer,font,270,270,makecol(0,0,255),-1," VIE: %d ",vie); 
textprintf_ex(Buffer,font,270,240,makecol(0,0,255),-1,"SCORE: %d",score); // afficher texte a lecran
}

if(key[KEY_C])
{
   p.x=0;
   p.y=400;
   j=0;
//clear_bitmap(Background);
Background = load_bitmap("./Images/level_desert.bmp", NULL);

}

if((get_pixel(Background, v.x,v.y)==255)&&(mouse_b & 1)&&(mouse_x>200)&&(mouse_x<700)&&(mouse_y<600)&&(mouse_y>300))
{






}

//bouton options

 if ((collision_background_left(v,BackgroundCol_p3,255))||(collision_background_right(v,BackgroundCol_p3,255))||(collision_background_up(v,BackgroundCol_p3,255))||(collision_background_down(v,BackgroundCol_p3,255)))
{
if((mouse_b & 1)&&(mouse_x>450)&&(mouse_x<750)&&(mouse_y<370)&&(mouse_y>270))
{
Background = load_bitmap("./Images/page_options.bmp", NULL);
}
}

if((get_pixel(Background, v.x,v.y)==255)&&(mouse_b & 1)&&(mouse_x>100)&&(mouse_x<330)&&(mouse_y<800)&&(mouse_y>600))
{
Background = load_bitmap("./Images/menu.bmp", NULL);
}

if((get_pixel(Background, v.x,v.y)==255)&&(mouse_b & 1)&&(mouse_x>730)&&(mouse_x<900)&&(mouse_y<800)&&(mouse_y>600))
{
exit(1);
}

//bouton quit

if ((collision_background_left(v,BackgroundCol_p3,255))||(collision_background_right(v,BackgroundCol_p3,255))||(collision_background_up(v,BackgroundCol_p3,255))||(collision_background_down(v,BackgroundCol_p3,255)))
{
if((mouse_b & 1)&&(mouse_x>450)&&(mouse_x<750)&&(mouse_y<450)&&(mouse_y>350))
{
exit(1);
}
}
		//blit Buffer to screen
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
        }

	destroy_background(Background);
	destroy_bitmap(Buffer);

	return (0);
}

END_OF_MAIN();
